// pages/goods_classify/goods_classify.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        successShow: false,
        sortTop: true,
        screen: false,
        navFixed: true,
        screenWidth: '',
        mengShow: false,
        navIndex: 0,
        outIndex: 0,
        nav: ['综合', '销量', '价格', '筛选'],
        screenList: [],
        goods: [],
        sort: 0,
        condition: [],
        typeid: 0,
        keyword: "",
        class_id:0,

        topListIndex:0,
        listIndex2:0,
        topList: [],

    },
    //选择一级分类下标
    chooseTopListIndex:function(e){
        var that=this;
        var index=e.currentTarget.id;
        var topList=that.data.topList;
        topList.map(elem=>{
            elem.ischoosed=false;
        })
        topList[index].ischoosed = true;
        that.setData({
            topListIndex:index,
            topList:topList
        })
    },
    //
    //选择二级分类下标
    chooseList2Index: function (e) {
        var that = this;
        var index = e.currentTarget.id;
        var topList = that.data.topList;
        var topListIndex=that.data.topListIndex;
        topList[topListIndex].list2.map(elem => {
            if(topList[topListIndex].list2.length>0){
              elem.ischoosed = false;
          }
        })
        topList[topListIndex].list2[index].ischoosed = true;
        that.setData({
            listIndex2: index,
            topList: topList
        })
    },
    getSearchValue: function(e) {
        this.setData({
            search_value: e.detail.value,
        })
    },
    search: function(e) {
        var that = this
        var keyword = that.data.search_value
        wx.navigateTo({
            url: '/pages/goods_classify/goods_classify?keyword=' + keyword,
        })
    },
    chooseNavIndex: function(e) {
        var that = this;
        var index = e.currentTarget.id;
        that.setData({
            navIndex: index,
            sort: index
        })
        if (index == 0 || index == 1) {
            that.setData({
                sort: index
            })
        }
        if (index == 2) {
            if (that.data.sortTop) {
                that.setData({
                    sortTop: false,
                    sort: 2
                })
            } else {
                that.setData({
                    sortTop: true,
                    sort: 3
                })
            }
        }
        if (index == 3) {
            that.setData({
                mengShow: true,
            })
        }

        if (index < 3) {
            //console.log(that.data.sort) 
            //获取商品列表
            that.goodsList();
        }
    },
    getScroll: function(e) {
        var that = this;
        // console.log(e.detail.scrollTop)
        if (e.detail.scrollTop >= (100 * that.data.screenWidth / 750)) {
            that.setData({
                navFixed: true,
            })
        } else {
            that.setData({
                navFixed: false,
            })
        }
    },
    getIndex: function(e) {
        var index = e.currentTarget.id;
        this.setData({
            outIndex: index,
        })
    },
    chooseScreenIndex: function(e) {
        var that = this;
        var index = e.currentTarget.id;
        var screenList = that.data.screenList;
        screenList[that.data.outIndex].list.forEach(item => {
            item.ischoosed = false;
        })
        screenList[that.data.outIndex].list[index].ischoosed = true;
        that.setData({
            screenList: screenList,
        })
    },
    sure2: function() {
        var that = this;
        var arr = [];
        var conStr = "";
        var screenList = that.data.screenList;
        screenList.forEach(item => {
            item.list.forEach(item => {
                if (item.ischoosed) {
                    //arr.push(item.id);
                    conStr += item.id + ",";
                }
            })
        })
        //console.log(arr)
        var class_id = 0;
        var topList = that.data.topList;
        var topListIndex = that.data.topListIndex;
        if (topList[topListIndex].ischoosed){
            class_id = item2.id
          }
      

        that.setData({
            sort: 4,
            condition: conStr,
            typeid: class_id,
        })
        that.setData({
            mengShow: false,
        })

        //获取商品列表
        that.goodsList();
    },
    close: function() {
        this.setData({
            mengShow: false,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        if (options.id) {
            that.setData({
                typeid: options.id,
            })
        }
        if (options.keyword) {
            that.setData({
                keyword: options.keyword,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    screenWidth: res.screenWidth,
                })
            },
        })

        //获取商品列表
        that.goodsList();

        //筛选条件
        wx.request({
            url: Host + 'Default/goodsCondition',
            data: {},
            success: function(res) {
                that.setData({
                    screenList: res.data.list,
                });
            },
        })

        //获取分类
        wx.request({
          url: Host + 'Default/category',
          data: {},
          success: function (res) {
            that.setData({
              topList: res.data.list,
            });
          },
        })
    },
    //获取商品列表
    goodsList: function() {
        var that = this;
        var openid = wx.getStorageSync('openid');
        var typeid = that.data.typeid;
        var sort = that.data.sort;
        var condition = that.data.condition;
        var keyword = that.data.keyword;
        that.setData({
            successShow: false,
        })
        wx.request({
            url: Host + 'Default/goodsList',
            data: {
                openid,
                typeid,
                sort,
                condition,
                keyword
            },
            success: function(res) {
                that.setData({
                    successShow: true,
                })
                that.setData({
                    goods: res.data.list,
                });
            },
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})